package com.test;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class HttpClientTest2 {
    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        for(int i=0;i<1000;i++){
            String uid = UUID.randomUUID().toString();
//            uid = "178a5fd05198454df15867b5fd7726e8112";
            //https://buy.bianxianmao.com/redict/?appKey=45cc2d9c0d144632b96f7a231e48117f&appType=weixin&appEntrance=11&business=money&uid=178a5fd05198454df15867b5fd7726e8&origin=http://m.cudaojia.com
            String url = "http://47.98.242.33:19822/activities/?appKey=45cc2d9c0d144632b96f7a231e48117f&appEntrance=13&business=money";
            String activityId = get302Url(url);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
            }
//            System.out.println(i+":"+activityId);
            Integer count = map.get(activityId);
            if(null == count){
                count = 1;
            }else{
                count++;
            }
            map.put(activityId,count);
        }
        for(Map.Entry entry:map.entrySet()){
            System.out.println("活动id:"+entry.getKey()+"   次数"+entry.getValue());
        }
    }

    public static String get(String url){
        CloseableHttpClient httpCilent = HttpClients.createDefault();//Creates CloseableHttpClient instance with default configuration.
        HttpGet httpGet = new HttpGet(url);
        try {
            CloseableHttpResponse response = httpCilent.execute(httpGet);
            try {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                System.out.println("--------------------------------------");
                // 打印响应状态
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    // 打印响应内容长度
                    System.out.println("Response content length: "
                            + entity.getContentLength());
                    // 打印响应内容
                    System.out.println("Response content: "
                            + EntityUtils.toString(entity));
                }
                System.out.println("------------------------------------");
            } finally {
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                httpCilent.close();//释放资源
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String get302Url(String url){
        CloseableHttpClient httpCilent = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpContext localContext= new BasicHttpContext();
            CloseableHttpResponse response = httpCilent.execute(httpGet,localContext);
            try {
                HttpUriRequest req = (HttpUriRequest)localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
                return   getActivityFromUrl(req.getURI().toString());
            } finally {
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                httpCilent.close();//释放资源
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String getActivityFromUrl(String url){
        String substr = url.substring(url.indexOf("activityid")+11);
        return substr.substring(0,substr.indexOf("&"));
    }
}
